/*
 * Copyright (c) 2009, Katholieke Universiteit Leuven
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of the Katholieke Universiteit Leuven nor the names of
 *       its contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * @file
 * Implementation for the core LooCI system
 * @author
 * Wouter Horré <wouter.horre@cs.kuleuven.be>
 */

#include "contiki.h"
#include "events/events.h"
#include "components/components.h"
#include "components/components_private.h"
#include "net/peers.h"
#include "net/networking_private.h"

#ifdef WITH_LOADABLE_COMPONENTS
#include <stdio.h>
#include "reconfiguration/componentstore_private.h"
#endif // WITH_LOADABLE_COMPONENTS

#ifdef WITH_COMMANDS
#include "commands/commands_private.h"
#endif // WITH_COMMANDS

#ifdef LOOCI_DEBUG
#include <stdio.h>
#ifdef CONTIKI_TARGET_AVR_RAVEN
#include <avr/pgmspace.h>
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
#else
#define PRINTF printf
#endif // CONTIKI_TARGET_AVR_RAVEN
#else
#define PRINTF(...)
#endif

PROCESS(looci, "Core LooCI process");

AUTOSTART_PROCESSES(&looci);

PROCESS_THREAD(looci, ev, data)
{
  PROCESS_BEGIN();

  PRINTF("LooCI initializing\r\n");

  // init peer library
  peer_init();

  PRINTF("Starting LooCI Event Manager\n");
  process_start(&looci_event_manager, NULL);

  PRINTF("Starting LooCI Networking Framework\r\n");
  process_start(&looci_networking_framework, NULL);

#ifdef WITH_LOADABLE_COMPONENTS
  // This one can start after the component manager since it is not used
  // by components
  PRINTF("Starting LooCI Component Store\r\n");
  process_start(&looci_component_store, NULL);
#endif // WITH_LOADABLE_COMPONENTS

#ifdef WITH_COMMANDS
  // This one can start after the component manager since it is not used
  // by components
  PRINTF("Starting LooCI Command Interface\r\n");
  process_start(&looci_commands, NULL);
#endif

  // This one is the last one to make sure everything is started
  // before the components
  PRINTF("Starting LooCI Component Manager\n");
  process_start(&looci_component_manager, NULL);

  PROCESS_END();
}

#ifdef WITH_LOADABLE_COMPONENTS
// using printf directly does not work for minimal-net target ...
void print_debug(char * s) {
  printf(s);
}
#endif //WITH_LOADABLE_COMPONENTS


